package com.example.b_308.myapplication.service;

import com.example.b_308.myapplication.comment.*;
import com.example.b_308.myapplication.comment.OfficeResult;
import com.example.b_308.myapplication.comment.utils.SharedPreferencesUtils;
import com.example.b_308.myapplication.service.base.BaseService;
import com.example.b_308.myapplication.model.Staff;

/**
 * Created by tongkang on 2017/11/10.
 */

public class UserService extends BaseService {
    static Staff userDao =new Staff();
    IUserView iUserView;

    public UserService(IUserView iUserView) {
        this.iUserView = iUserView;
//        userDao = SharedPreferencesUtils.getObj(OfficeApplication.getOfficeApplication(),Staff.USER_TAG,Staff.class);
    }

    public void login(Staff staff){
        userDao.postData("/user/login", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if(OfficeContent.SUCCESS_CODE == officeResult.code){
                    userDao =new Staff(officeResult.data);
                    SharedPreferencesUtils.putObj(OfficeApplication.getOfficeApplication(),Staff.USER_TAG,userDao);
                    iUserView.loginSuccess(userDao);
                }else {
                    iUserView.onFail(officeResult.message);
                }

            }
        });

    }

    public void register(Staff staff){
        userDao.postData("/user/register", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if(OfficeContent.SUCCESS_CODE == officeResult.code){
                    userDao =new Staff(officeResult.data);
                    iUserView.registerSuccess();
                }else {
                    iUserView.onFail(officeResult.message);
                }

            }
        });

    }

    public  void getCode(Staff staff){
        userDao.postData("/user/getCode", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if(OfficeContent.SUCCESS_CODE == officeResult.code){
                    userDao =new Staff(officeResult.data);
                    iUserView.getCodeSuccess();
                }else {
                    iUserView.onFail(officeResult.message);
                }

            }
        });

    }

    public void loginDefault(){
        userDao.postData("/user/loginDefault", userDao, new INetCallBack() {
            @Override
            public void onNetCallBack(OfficeResult officeResult) {
                if(OfficeContent.SUCCESS_CODE == officeResult.code){
                    userDao =new Staff(officeResult.data);
                }else {
                    iUserView.onFail(officeResult.message);
                }

            }
        });


    }
    @Override
    protected void onStart() throws Exception {

    }

   @Override
   protected void onStop() throws Exception {

   }

}
